package com.douwong.student.popuwindow;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.douwong.student.R;
import com.douwong.student.interfaces.ColorChangedListener;
import com.douwong.student.view.ColorChangeView;
import com.douwong.student.view.ColorPickerView;

/*
颜色选择器
 */
public class ColorPickerPopuwindow extends PopupWindow {

	private Context context;
	private ColorPickerView colorpickerview;
	private View view;
	private SeekBar paint_seekbar;
	private ColorChangedListener colorChange;
	private ColorChangeView color_change_text;
	private int[]colors=new int[]{R.id.color_1_text,R.id.color_2_text,R.id.color_3_text,R.id.color_4_text,
		R.id.color_5_text,R.id.color_6_text,R.id.color_7_text,R.id.color_8_text,R.id.color_9_text,R.id.color_10_text,
		R.id.color_11_text,R.id.color_12_text};
	private int size=5;
	private int Changecolor;

	public ColorPickerPopuwindow(Context context) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.context = context;
		view = inflater.from(context).inflate(R.layout.color_layout, null);
		initView();
		paint_seekbar = (SeekBar) view.findViewById(R.id.paint_seekbar);
		colorpickerview = (ColorPickerView) view
				.findViewById(R.id.colorpickerview);
		color_change_text = (ColorChangeView) view
				.findViewById(R.id.color_change_text);
		Changecolor=context.getResources().getColor(R.color.color_11);
		paint_seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {

			}

			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {

			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				size=progress;
				if(size<3){
					size=3;
				}
//				if(size>15){
//					size=15;
//				}
				colorChange.sizeChanged(size);
				color_change_text.setColor(Changecolor,size);
			}
		});
		colorpickerview.setColorChangedListener(new ColorPickerView.onColorChangedListener() {

			@Override
			public void colorChanged(int red, int blue, int green, int pixel) {
				Log.e("ColorPickerPopuwindow", pixel + "");
				if(pixel!=0){
					Changecolor=pixel;
					colorChange.colorChanged(pixel);
					color_change_text.setColor(pixel,size);
				}
				
			}
		});
		this.setContentView(view);
		this.setWidth(LayoutParams.WRAP_CONTENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);
		// 设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(false);
		ColorDrawable dw = new ColorDrawable(0x00000000);
		this.setBackgroundDrawable(dw);
	}

	// 初始化
	private void initView() {
		ColorOnClick listener = new ColorOnClick();
		for (int i = 0; i < colors.length; i++) {
			view.findViewById(colors[i]).setOnClickListener(listener);
		}
	}

	public void setOnColorChangerLister(ColorChangedListener colorChange) {
		this.colorChange = colorChange;
	}

	private class ColorOnClick implements OnClickListener {
		@Override
		public void onClick(View v) {
			int color = 0;
			switch (v.getId()) {
			case R.id.color_1_text:
				color = context.getResources().getColor(R.color.color_1);
				break;
			case R.id.color_2_text:
				color = context.getResources().getColor(R.color.color_2);
				break;
			case R.id.color_3_text:
				color = context.getResources().getColor(R.color.color_3);
				break;
			case R.id.color_4_text:
				color = context.getResources().getColor(R.color.color_4);
				break;
			case R.id.color_5_text:
				color = context.getResources().getColor(R.color.color_5);
				break;
			case R.id.color_6_text:
				color = context.getResources().getColor(R.color.color_6);
				break;
			case R.id.color_7_text:
				color = context.getResources().getColor(R.color.color_7);
				break;
			case R.id.color_8_text:
				color = context.getResources().getColor(R.color.color_8);
				break;
			case R.id.color_9_text:
				color = context.getResources().getColor(R.color.color_9);
				break;
			case R.id.color_10_text:
				color = context.getResources().getColor(R.color.color_10);
				break;
			case R.id.color_11_text:
				color = context.getResources().getColor(R.color.color_11);
				break;
			case R.id.color_12_text:
				color = context.getResources().getColor(R.color.color_12);
				break;
			default:
				break;
			}
			Changecolor=color;
			colorChange.colorChanged(color);
			color_change_text.setColor(color,size);
		}
	}


	

}
